package com.cuij.servicelmpl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cuij.dao.orderBusiDao;
import com.cuij.dao.orderGoodsDao;
import com.cuij.entity.Result;
import com.cuij.entity.orderBusi;
import com.cuij.service.orderBusiService;

@Service
public class orderBusiServicelmpl implements orderBusiService {

    @Resource
    private orderBusiDao orderbusidao;

    @Resource
    private orderGoodsDao ordergoodsdao;

    public Result findBusiIdByUseId(String use_id) {
        Result result = new Result();
        String busi_id = orderbusidao.findBusiIdByUserId(use_id);
        if (null != busi_id || !"".equals(busi_id)) {
            result.setData(busi_id);
        }
        return result;
    }

    // 查询商户下基本信息
    public Result findBusiInfoByBusiId(String busi_id) {
        Result result = new Result();
        Map busiinfo = orderbusidao.findBusiInfoByBusiId(busi_id);
        if (null != busiinfo && busiinfo.size() > 0) {
            result.setStatus(0);
            result.setMsg("查询成功");
            result.setData(busiinfo);
        }
        else {
            result.setStatus(1);
            result.setMsg("无该商家信息，请刷新");
        }
        return result;
    }

    // 修改商家信息
    public Result modifyBusiInfo(String busi_id, String busi_type, String busi_desc, String busi_pic,
            String busi_linkname, String busi_linkphone, String busi_address, String busi_atten, String oldfilename) {
        Result result = new Result();
        orderBusi orderbusi = new orderBusi();
        if (null != busi_pic && !"".equals(busi_pic)) {
            orderbusi.setBusi_pic(busi_pic);
            File oldfile = new File("D:/MeCode/SchoolOrder/WebRoot/busipics/" + oldfilename);
            if (oldfile.isFile() && oldfile.exists()) {
                oldfile.delete();
            }
        }
        if (null != busi_type && !"".equals(busi_type)) {
            orderbusi.setBusi_type(busi_type);
        }
        orderbusi.setBusi_id(busi_id);
        orderbusi.setBusi_desc(busi_desc);
        orderbusi.setBusi_linkname(busi_linkname);
        orderbusi.setBusi_linkphone(busi_linkphone);
        orderbusi.setBusi_address(busi_address);
        orderbusi.setBusi_atten(busi_atten);
        // 进行入库操作
        orderbusidao.modifyBusiInfo(orderbusi);
        return result;
    }

    public Result querybusimoney(String busi_id) {
        Result result = new Result();
        String money = orderbusidao.querybusimoney(busi_id);
        result.setStatus(0);
        result.setMsg("查询成功");
        result.setData(money);
        return result;
    }

    // 查询所有商家
    public Result querybusi() {
        Result result = new Result();
        List<Map<String, Object>> busilist = orderbusidao.querybusi();
        if (busilist != null && busilist.size() > 0) {
            result.setStatus(0);
            result.setData(busilist);
        }
        return result;
    }

    public Result querybusibytype(String type) {
        Result result = new Result();
        List<Map<String, Object>> busiList = new ArrayList<Map<String, Object>>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("type", type);
        // 查询商家
        busiList = orderbusidao.querybusibytype(param);
        if (null != busiList && busiList.size() > 0) {
            result.setData(busiList);
            result.setStatus(0);
        }
        else {
            result.setStatus(1);
        }
        return result;
    }

    public Result querybusibyterm(String type, String term) {
        Result result = new Result();
        List<Map<String, Object>> busiList = new ArrayList<Map<String, Object>>();
        Map<String, Object> param = new HashMap<String, Object>();
        if ("全部商家".equals(type)) {
            if ("默认排序".equals(term)) {
                busiList = orderbusidao.querybusi();
            }
            else {
                if ("按销量".equals(term)) {
                    param.put("term", "BUSI_COUNT");
                }
                if ("按评价".equals(term)) {
                    param.put("term", "BUSI_LEVEL");
                }
                if ("配送速度".equals(term)) {
                    param.put("term", "BUSI_SPEED");
                }
                busiList = orderbusidao.allbusibyterm(param);
            }
        }
        else {
            param.put("type", type);
            if ("默认排序".equals(term)) {
                busiList = orderbusidao.querybusibytype(param);
            }
            else {
                if ("按销量".equals(term)) {
                    param.put("term", "BUSI_COUNT");
                }
                if ("按评价".equals(term)) {
                    param.put("term", "BUSI_LEVEL");
                }
                if ("配送速度".equals(term)) {
                    param.put("term", "BUSI_SPEED");
                }
                busiList = orderbusidao.querybusibyterm(param);
            }
        }
        if (null != busiList && busiList.size() > 0) {
            result.setData(busiList);
            result.setStatus(0);
        }
        else {
            result.setStatus(1);
        }
        return result;
    }

}
